package com.bk.bookviews.controller;

import com.bk.bookviews.domain.Notice;
import com.bk.bookviews.domain.ResultMsg;
import com.bk.bookviews.exception.BusinessException;
import com.bk.bookviews.service.NoticeService;
import com.bk.bookviews.util.PageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bk/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @GetMapping()
    public ResultMsg selectNotice(Integer id) {
        Notice notice = noticeService.selectNotice(id);
        return ResultMsg.successResult(notice);
    }

    @PostMapping("/save")
    public ResultMsg saveNotice(@RequestBody Notice notice,@RequestHeader("token") String token){
        noticeService.saveNotice(notice,token);
        return ResultMsg.successResult();
    }

    @PostMapping("/list")
    @ResponseBody
    public ResultMsg selectNoticeList(@RequestBody Notice notice,@RequestHeader("pageNum") String pageNum, @RequestHeader("pageSize") String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        return PageUtils.getPagination(noticeService.selectNoticeList(notice));
    }


    /**
     * 删除
     */
    @DeleteMapping()
    public ResultMsg delete(Integer id) throws BusinessException {
        noticeService.delete(id);
        return ResultMsg.successResult();
    }
}
